﻿using System;
using System.Collections.Generic;
using System.Text;

namespace dotHTML.HTML {
    /// <summary>
    /// Represents a link to another resource.
    /// </summary>
    [Serializable]
    [Standalone]
    public class Link : Element {
        private string _charset;
        private string _href;
        private string _hreflang;
        private string _media;
        private ICollection<string> _rel;
        private ICollection<string> _rev;
        private ICollection<string> _sizes;
        private string _type;

        /// <summary>
        /// Constructs a new link object with the given source URL.
        /// </summary>
        /// <param name="href"></param>
        public Link(string href) {
            _rel = new LinkedList<string>();
            _rev = new LinkedList<string>();
            _sizes = new LinkedList<string>();
            _href = href;
        }

        /// <summary>
        /// Determines the encoding of the target resource.
        /// </summary>
        [Obsolete("Deprecated in HTML5 - use the HTTP Content-Type header instead.")]
        public string Charset {
            get { return _charset; }
            set { _charset = value; }
        }

        /// <summary>
        /// Determines the URL of the target resource.
        /// </summary>
        public string Href {
            get { return _href; }
            set { _href = value; }
        }

        /// <summary>
        /// Determines the language of the target resource.
        /// </summary>
        public string HrefLang {
            get { return _hreflang; }
            set { _hreflang = value; }
        }

        /// <summary>
        /// Represents a media query for the target resource.
        /// </summary>
        public string Media {
            get { return _media; }
            set { _media = value; }
        }

        /// <summary>
        /// Determines how this resource is related to the target resource.
        /// </summary>
        public ICollection<string> Rel {
            get { return _rel; }
        }

        /// <summary>
        /// Determines how the target resource is related to this resource.
        /// </summary>
        [Obsolete("Deprecated - use the corresponding \"rel\" attributes instead.")]
        public ICollection<string> Rev {
            get { return _rev; }
        }

        /// <summary>
        /// Determines the sizes of the target resources, in the format AxB.
        /// </summary>
        public ICollection<string> Sizes {
            get { return _sizes; }
        }

        /// <summary>
        /// Determines the MIME type of the target resource.
        /// </summary>
        public string Type {
            get { return _type; }
            set { _type = value; }
        }

        /// <summary>
        /// Gets a formatted string of all child elements of this element.
        /// </summary>
        /// <param name="sb">The StringBuilder to write this element's child
        /// data to.</param>
        protected override void WriteLocalAttributes(StringBuilder sb) {
            AttributeWriter.Write(sb, _charset, "charset");
            AttributeWriter.Write(sb, _href, "href");
            AttributeWriter.Write(sb, _hreflang, "hreflang");
            AttributeWriter.Write(sb, _media, "media");
            AttributeWriter.Write(sb, _rel, "rel", ' ');
            AttributeWriter.Write(sb, _rev, "rev", ' ');
            AttributeWriter.Write(sb, _sizes, "sizes", ' ');
            AttributeWriter.Write(sb, _type, "type");
        }
    }
}
